package cn.smart.epic.common.constant.factory;

import cn.smart.epic.common.constant.state.ManagerStatus;
import cn.smart.epic.common.persistence.dao.*;
import cn.smart.epic.common.persistence.model.Dept;
import cn.smart.epic.common.persistence.model.Dict;
import cn.smart.epic.common.persistence.model.Role;
import cn.smart.epic.common.persistence.model.User;
import cn.smart.epic.core.util.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther Uncle_Z
 * @date 2017/9/22 23:30
 * @Email:2545375@qq.com
 * @Descrition
 * @since 1.0
 */

@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {

    private UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
    private DictMapper dictMapper = SpringContextHolder.getBean(DictMapper.class);
    private RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);
    private DeptMapper deptMapper = SpringContextHolder.getBean(DeptMapper.class);

    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }

    @Override
    public String getUserNameById(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (user != null) {
            return user.getName();
        } else {
            return "--";
        }
    }

    @Override
    public String getUserAccountById(int userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (user != null) {
            return user.getAccount();
        } else {
            return "--";
        }
    }


    @Override
    public String getRoleName(String roleIds) {
        return null;
    }

    /**
     * 通过角色id获取角色名称
     * @param roleId
     * @return
     */
    @Override
    public String getSingleRoleName(int roleId) {
        if (roleId==0) {
            return "--";
        }
        Role roleObj = roleMapper.selectByPrimaryKey(roleId);
        if (roleObj != null && StringUtils.isNotEmpty(roleObj.getName())) {
            return roleObj.getName();
        }
        return "";
    }

    /**
     * 通过角色id获取角色英文名称
     * @param roleId
     * @return
     */
    @Override
    public String getSingleRoleTip(Integer roleId) {
        if (roleId==0) {
            return "--";
        }
        Role roleObj = roleMapper.selectByPrimaryKey(roleId);
        if (roleObj!=null && StringUtils.isNotEmpty(roleObj.getName())) {
            return roleObj.getTips();
        }
        return "";
    }

    @Override
    public String getDeptName(Integer deptId) {
        return null;
    }

    @Override
    public String getDictName(int dictId) {
        if (dictId == 0) {
            return "";
        } else {
            Dict dict = dictMapper.selectByPrimaryKey(dictId);
            if (dict != null) {
                return dict.getName();
            } else {
                return "";
            }
        }
    }

    @Override
    public String getNoticeTitle(Integer dictId) {
        return null;
    }

    @Override
    public String getDictsByName(String name, Integer val) {
        Dict dict = dictMapper.selectByName(name);
        if (dict == null) {
            return "";
        } else {
            Example example = new Example(Dict.class);
            example.createCriteria().andEqualTo("pid", dict.getId());
            List<Dict> dicts = dictMapper.selectByExample(example);
            return dicts.stream().filter(item -> (item.getNum() != null && item.getNum().equals(val)))
                    .map(Dict::getName)
                    .findAny().orElse("");
        }

    }

    /**
     * 获取性别名称
     *
     * @param sex
     * @return
     */
    @Override
    public String getSexName(Integer sex) {
        return getDictsByName("性别", sex);
    }

    /**
     * 获取用户登录状态
     *
     * @param status
     * @return
     */
    @Override
    public String getStatusName(Integer status) {
        return ManagerStatus.valueOf(status);
    }

    /**
     * 查询字典
     *
     * @param id
     * @return
     */
    @Override
    public List<Dict> findInDict(Integer id) {
        if (id == null) {
            return null;
        }else{
            Example example=new Example(Dict.class);
            example.createCriteria().andEqualTo("pid",id);
            List<Dict> dicts = dictMapper.selectByExample(example);
            if (dicts == null || dicts.size()==0) {
                return null;
            }else{
                return dicts;
            }
        }
    }

    @Override
    public String getCacheObject(String para) {
        return null;
    }

    @Override
    public List<Integer> getSubDeptId(Integer deptid) {
        Example example = new Example(Dept.class);
        example.createCriteria().andLike("pids", "%[" + deptid + "]%");

        List<Dept> depts = this.deptMapper.selectByExample(example);

        ArrayList<Integer> deptids = new ArrayList<>();

        if(depts != null || depts.size() > 0){
            for (Dept dept : depts) {
                deptids.add(dept.getId());
            }
        }

        return deptids;
    }
}
